﻿using System;
using System.Data;
using System.Windows.Forms;
using Newtonsoft.Json;
using Oracle.ManagedDataAccess.Client;
using Wantn.Core;
using Wantn.DAS.Msg.Common;

namespace Wantn.LMS
{
    /// <summary>
    /// 样品交接
    /// </summary>
    public partial class FrmReciveSample : Form
    {
        /// <summary>
        /// 服务器地址
        /// </summary>
        public string Url = "";

        /// <summary>
        /// 默认构造方法
        /// </summary>
        public FrmReciveSample()
        {
            InitializeComponent();
        }

        private void btn_Recive_Click(object sender, EventArgs e)
        {
            DoReciveSample();
        }

        private void DoReciveSample()
        {
            var strRfid = txt_Rfid.Text.Trim();
            var strBatchNo = txt_BatchNo.Text.Trim();
            var msg = new DasMsgExecProc
            {
                ProcName = "PCK_ASSAYMANAGE.RECIVESAMPLE",
                ProcParams = new[]
                {
                    new ProcParam
                    {
                        DbType = Convert.ToInt32(OracleDbType.Varchar2),
                        Direction = ParameterDirection.Input,
                        Name = "P_RFID",
                        Size = null,
                        Value = strRfid
                    },
                    new ProcParam
                    {
                        DbType = Convert.ToInt32(OracleDbType.Decimal),
                        Direction = ParameterDirection.Input,
                        Name = "P_BATCHNO",
                        Size = null,
                        Value = strBatchNo
                    },
                    new ProcParam
                    {
                        DbType = Convert.ToInt32(OracleDbType.Decimal),
                        Direction = ParameterDirection.Input,
                        Name = "P_ASSNO",
                        Size = null,
                        Value = DateTime.Now.ToString("yyyyMMddHHmmss")
                    },
                    new ProcParam
                    {
                        DbType = Convert.ToInt32(OracleDbType.Decimal),
                        Direction = ParameterDirection.Input,
                        Name = "P_USERID",
                        Size = null,
                        Value = WinFormSys.FrmBase.UserInfo.UserId
                    },
                    new ProcParam
                    {
                        DbType = Convert.ToInt32(OracleDbType.Decimal),
                        Direction = ParameterDirection.Output,
                        Name = "P_ERRCODE",
                        Size = null,
                        Value = null
                    }, 
                    new ProcParam
                    {
                        DbType = Convert.ToInt32(OracleDbType.Varchar2),
                        Direction = ParameterDirection.Output,
                        Name = "P_ERRMSG",
                        Size = 2000,
                        Value = null
                    }
                }
            };
            var reqContentPlain = JsonConvert.SerializeObject(msg);
            var dasClientResult = DasClinet.SendPost(Url + "/common/execproc.do", 30 * 1000, true, WinFormSys.FrmBase.UserInfo.UserName,
                WinFormSys.FrmBase.UserInfo.UserPwd, reqContentPlain);
            if (!dasClientResult.IsSuccess || dasClientResult.StatusCode != 1)
            {
                MessageBox.Show(@"交接样品发生错误:\n 错误1:" + dasClientResult.ErrInfo + "\n 错误2:" + dasClientResult.StatusDesc);
                return;
            }
            var jsonSet = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
            msg = JsonConvert.DeserializeObject<DasMsgExecProc>(dasClientResult.ResponseContentPlain, jsonSet);
            if (Convert.ToInt32(msg.ProcParams[2].Value) != 0)
            {
                MessageBox.Show(@"交接样品发生错误:" + msg.ProcParams[3].Value);
                return;
            }
            MessageBox.Show(@"交接样品成功!");
        }

        private void btn_Ok_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
    }
}
